package org.jeecg.modules.zhoupackage.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricColor;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricInfo;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricType;
import org.jeecg.modules.zhoupackage.service.IChooseStoreService;
import org.jeecg.modules.zhoupackage.service.IMlccFabricInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 面料下单Controller
 *
 * @author zxl
 * @since 20200922
 */
@RestController
@RequestMapping("/fabric")
public class ChooseStoreController {

    @Autowired
    IChooseStoreService service;

    @Autowired
    IMlccFabricInfoService fabricInfoService;

    /**
     * 获取面料列表
     *
     * @param entity   查询条件
     * @param pageNo   当前页
     * @param pageSize 页面大小
     */
    @RequestMapping("/getFabricList")
    public JSONObject getFabricList(MlccFabricInfo entity, @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "30") int pageSize) {
        Page<MlccFabricInfo> page = new Page<>(pageNo, pageSize);
        JSONObject jsonObject = new JSONObject();
        try {
            IPage<MlccFabricInfo> fabricList = fabricInfoService.getFabricList(entity, page);
            jsonObject.put("code", 0);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", fabricList);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
            return jsonObject;
        }
    }

    /**
     * 面料下单页面获取面料类型列表
     *
     * @return
     */
    @RequestMapping("/getFabricTypeList")
    public JSONObject getFabricTypeList() {
        JSONObject jsonObject = new JSONObject();
        try {
            List<MlccFabricType> types = fabricInfoService.getFabricType();
            jsonObject.put("code", 0);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", types);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
            return jsonObject;
        }
    }

    /**
     * 获取颜色选项
     *
     * @return
     */
    @RequestMapping("/getColorList")
    public JSONObject getColorList() {
        JSONObject jsonObject = new JSONObject();
        try {
            List<MlccFabricColor> colorList = fabricInfoService.getColorList();
            jsonObject.put("code", 0);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", colorList);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
            return jsonObject;
        }
    }

    /**
     * 根据面料ID获取面料详情
     *
     * @param id 面料ID
     * @return
     */
    @RequestMapping("/getFabricDetailByID")
    public JSONObject getFabricDetailByID(int id) {
        JSONObject jsonObject = new JSONObject();
        try {
            MlccFabricInfo detail = fabricInfoService.getFabricDetailByID(id);
            jsonObject.put("code", 0);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", detail);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
            return jsonObject;
        }
    }

    /**
     * 编辑面料订单备注
     *
     * @param orderSn 订单编号
     * @param remark  备注信息
     */
    @RequestMapping("/updateFabricRemark")
    public JSONObject updateFabricRemark(String orderSn, String remark) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = fabricInfoService.updateFabricRemark(orderSn, remark);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "查询成功");
            } else {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
        }
        return jsonObject;
    }
}
